import aioredis
from fastapi_admin.app import app as admin_app
from fastapi_admin.providers.login import UsernamePasswordProvider
from .user.models import User

async def configure_admin():
    redis_client = await aioredis.from_url(
        "redis://:sihengroot@redis.service-test.oigbuy.com:6379",
        encoding="utf-8",
        decode_responses=True
    )

    await admin_app.configure(
        redis=redis_client,
        logo_url="https://example.com/logo.png",
        template_folders=[],
        providers=[
            UsernamePasswordProvider(
                admin_model=User
            )
        ]
    )
async def create_admin_user():
    user = await User.get_or_none(username="admin")
    if not user:
        await User.create_user(
            username="admin",
            password="admin123",  # 确保密码是加密的
            is_superuser=True
        )
